    //
//  CourseDetailViewController.m
//  studip
//
//  Created by anti on 05.07.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CourseDetailViewController.h"


@implementation CourseDetailViewController

@synthesize courseId = _courseId;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (id)initWithCourseId:(NSString*)courseId {
	// Note the [self init] here instead of [super init].
	if (self = [self init]) {
		self.courseId = courseId;
		
		CourseModel* course = [[CourseModel alloc] initWithCourseId: courseId];
		
		NSMutableArray* items = [[NSMutableArray  alloc] init];
		NSMutableArray* sections = [[NSMutableArray  alloc] init];
		NSMutableArray* itemRows = [[NSMutableArray  alloc] init];
		
		self.title = course.courseName;
		
		[sections addObject:@"Details"];
		
		[itemRows addObject:[TTTableCaptionItem
							 itemWithText: course.courseName
							 caption: @"Name"]];
		
		[itemRows addObject:[TTTableCaptionItem
							 itemWithText: course.courseDescription
							 caption: @"Description"]];
		
		[itemRows addObject:[TTTableCaptionItem
							 itemWithText: course.courseNumber
							 caption: @"Course No"]];
		
		[items addObject: itemRows];

		if ([course.courseTimes count] > 0) {
			
			[sections addObject:@"Dates"];
			
			TT_RELEASE_SAFELY(itemRows);
			itemRows = [[NSMutableArray  alloc] init];
			
			for(int i = 0; i < [course.courseTimes count]; i++){
				
				[itemRows addObject:[TTTableTextItem
									 itemWithText: [course.courseTimes objectAtIndex: i]]];
				
			}

			[items addObject: itemRows];
		}
		
		if ([course.courseInstructors count] > 0) {
			
			[sections addObject:@"Instructors"];
			
			TT_RELEASE_SAFELY(itemRows);
			itemRows = [[NSMutableArray  alloc] init];
			
			for(int i = 0; i < [course.courseInstructors count]; i++){
				
				[itemRows addObject:[TTTableTextItem
									 itemWithText: [course.courseInstructors objectAtIndex: i]]];
				
			}
			
			[items addObject: itemRows];
		}
		
		if ([course.courseTutors count] > 0) {
			
			[sections addObject:@"Tutors"];
			
			TT_RELEASE_SAFELY(itemRows);
			itemRows = [[NSMutableArray  alloc] init];
			
			for(int i = 0; i < [course.courseTutors count]; i++){
				
				[itemRows addObject:[TTTableTextItem
									 itemWithText: [course.courseTutors objectAtIndex: i]]];
				
			}
			
			[items addObject: itemRows];
			
		}
		
		
		[sections addObject:@"More"];
		
		TT_RELEASE_SAFELY(itemRows);
		itemRows = [[NSMutableArray  alloc] init];
		
			
		NSMutableString* filePath = [[NSMutableString alloc] initWithString: @"fb://courseDetails/"];
		[filePath appendString:course.courseId];
		[filePath appendString: @"/files/"];
		
		[itemRows addObject:[TTTableTextItem
							 itemWithText: @"Files"
							 URL: filePath
							 accessoryURL: filePath]];
		
		[items addObject: itemRows];
		
		TTSectionedDataSource* ds = [[TTSectionedDataSource alloc] autorelease];
		ds.sections = sections;
		ds.items = items;
		self.dataSource = ds;
		
		TT_RELEASE_SAFELY(sections);
		TT_RELEASE_SAFELY(items);
		
	}
	
	return self;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)popToLauncherView
{
  [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
  [super viewWillDisappear:animated];
  
  [self.navigationController setToolbarHidden:YES animated:YES];
}

- (void)addCourse
{
  [[NSNotificationCenter defaultCenter]
   postNotificationName:@"addDesigningUserExperiences" object:self];
  UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Course added" message:@"Designing user experiences was added to your home screen for quick navigation." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
  [alert show];
}

- (void)loadView
{
	[super loadView];
  
  [self.navigationController setToolbarHidden:NO animated:YES];
  self.toolbarItems = [NSArray arrayWithObject:[[[UIBarButtonItem alloc] 
                                                 initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                 target:self action:@selector(addCourse)] autorelease]];
  
  [self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"eingangsbildschirm"] style:UIBarButtonItemStyleBordered target:self action:@selector(popToLauncherView)] autorelease] animated:YES];
}

- (id)init {
	if (self = [super init]) {
		self.tableViewStyle = UITableViewStyleGrouped;
		self.variableHeightRows = YES;
		self.title = @"Course details";
	}
	
	return self;
}

- (void)dealloc {
	TT_RELEASE_SAFELY(_courseId);
    [super dealloc];
}


@end
